# -*- coding: utf-8 -*-
# 创建时间：2021/7/16 15:52
from bs4 import BeautifulSoup
import requests
import time, math, random, json
import pymysql
import re
from xlwt import Workbook

__author__ = 'LuckyHhy'
# 爬取github 最火的python 项目

# 获取数据
def get_data():
    base_url = "https://api.github.com/search/repositories?q=language:python+created:%3E2019-12-31&sort=stars&order=desc&per_page=10000"
    # base_url = url + '&' + query_string
    response = requests.get(base_url)
    result = response.json()
    data = []
    for item in result['items']:
        ck = [item['name'], item['description'], item['html_url'], item['stargazers_count'], item['watchers_count'],
              item['forks_count'], item['created_at']
            , item['updated_at']]
        data.append(ck)
    return data


# 保存到excel 文件
def save_xls(pathh):
    w = Workbook()
    ws = w.add_sheet('datas')  # 创建一个工作表
    ws.write(0, 0, '项目名称')
    ws.write(0, 1, '项目描述')
    ws.write(0, 2, '项目地址')
    ws.write(0, 3, '项目stars数')
    ws.write(0, 4, '项目watchers数')
    ws.write(0, 5, '项目forks数')
    ws.write(0, 6, '创建时间')
    ws.write(0, 7, '更新时间')

    results = get_data()
    i = 1
    for row in results:
        g = 0
        # print(f"正在执行第{i}行数据")
        for jj in row:
            ws.write(i, g, jj)  # 行 ，列  数据
            g = g + 1
        i = i + 1

    print("已经跳出来了")
    w.save(pathh)


def main():
    pathh = 'github最火的python项目.xls'  # 存放路径
    save_xls(pathh)
    print("下载结束")


if __name__ == '__main__':
    main()
